package com.feonix.complat.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.feonix.common.exception.OperationException;
import com.feonix.common.listener.UserSessionInfo;
import com.feonix.common.util.NumberUtil;
import com.feonix.common.util.StringUtil;
import com.feonix.complat.dao.RightDAO;
import com.feonix.complat.dao.RoleRightDAO;
import com.feonix.complat.entity.Right;
import com.feonix.complat.entity.Role;
import com.feonix.complat.formbean.RightFormBean;
import com.feonix.support.CurrentUser;

/**
 * 用户权限
 * @author Feonixzw
 *
 */
@Service("rightService")
public class RightService {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RightDAO rightDAO;
	
	@Autowired
	private RoleRightDAO roleRightDAO;
	
	/**
	 * 检查用户是否拥有某权限
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param rightId
	 *            权限ID
	 * @return true - 有权限<br/>
	 *         false - 无权限
	 */
	public static boolean haveThisRight(String rightId) {
		/* 取得用户的权限 */
		CurrentUser user = UserSessionInfo.getCurrentUser();
		if (user == null) {
			return false;
		}
		List<Right> rightList = user.getRightList();
		/* 判断用户是否拥有roleId 的权限 */
		for (Right right : rightList) {
			if (right != null && right.getModuleId().equals(rightId)) {
				return true;
			}
		}

		return false;
	}
	
	/**
	 * 获得用户有权限的模块
	 * 
	 * @param userId
	 *            用户ID
	 * @return
	 */
	public List<Right> findUserRights(int userId) {
		if (NumberUtil.getInt(userId) <= 0) {
			return null;
		}

		Integer groupId = userService.findGroupIdByIid(userId);
		List<Right> rightList = rightDAO.findByUserIdOrGroupId(userId, groupId);
		return rightList;
	}
	
	/**
	 * 获得所有权限集合
	 * 
	 * @return 权限实体集合
	 */
	public List<Right> findAllRights() {
		List<Right> rightList = rightDAO.findAll();
		return rightList;
	}

	/**
	 * 获得角色的权限集合
	 * 
	 * @param roleId
	 *            角色ID
	 * @return 权限实体集合
	 */
	public List<Right> findByRoleId(int roleId) {
		if (roleId <= 0) {
			return null;
		}
		List<Right> rightList = rightDAO.findByRoleId(roleId);

		return rightList;
	}

	/**
	 * 根据主键iid获取 权限
	 * @param iid
	 * @return
	 */
	public Right findByIid(int iid) {
		return rightDAO.findByIid(iid);
	}

	/**
	 * 插入
	 * @param rightFormBean
	 * @return
	 */
	public boolean add(RightFormBean rightFormBean) {
		int iid = rightDAO.insert(rightFormBean);
		return iid > 0? true : false;
	}

	/**
	 * @param rightFormBean
	 * @return
	 */
	public boolean modify(RightFormBean rightFormBean) {
		if(rightFormBean == null || rightFormBean.getIid() <= 0){
			return false;
		}
		return rightDAO.update(rightFormBean);
	}

	/**
	 * 根据ids删除记录
	 * @param ids
	 * @return
	 */
	public boolean removeByIds(String ids) {
		List<Integer> idsList = StringUtil.toIntegerList(ids, ",");
		if (CollectionUtils.isEmpty(idsList)) {
			return false;
		}
		boolean isSuccess = false;
		isSuccess = rightDAO.deleteByIds(idsList);
		if(isSuccess){
			isSuccess = roleRightDAO.deleteByRightIds(idsList);
		}
		if(!isSuccess){
			throw new OperationException("功能权限删除失败！");
		}
		return isSuccess;
	}

}
